package com.ruoyi.project.common.lesson.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.common.lesson.domain.ZyLesson;
import com.ruoyi.project.common.lesson.service.ILessonService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * lessonController
 * 
 * @author shixiangwen
 * @date 2020-06-03
 */
@Controller
@RequestMapping("/common/lesson")
public class ZyLessonController extends BaseController {
	private String prefix = "common/lesson";

	@Autowired
	private ILessonService lessonService;

	@RequiresPermissions("common:lesson:view")
	@GetMapping()
	public String lesson() {
		return prefix + "/lesson";
	}

	/**
	 * 查询lesson列表
	 */
	@RequiresPermissions("common:lesson:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ZyLesson lesson) {
		startPage();
		List<ZyLesson> list = lessonService.selectLessonList(lesson);
		return getDataTable(list);
	}

	/**
	 * 导出lesson列表
	 */
	@RequiresPermissions("common:lesson:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ZyLesson lesson) {
		List<ZyLesson> list = lessonService.selectLessonList(lesson);
		ExcelUtil<ZyLesson> util = new ExcelUtil<ZyLesson>(ZyLesson.class);
		return util.exportExcel(list, "lesson");
	}

	/**
	 * 新增lesson
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存lesson
	 */
	@RequiresPermissions("common:lesson:add")
	@Log(title = "lesson", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ZyLesson lesson) {
		lesson.setCreatedTime(DateUtils.getNowDate());
		return toAjax(lessonService.insertLesson(lesson));
	}

	/**
	 * 修改lesson
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		ZyLesson lesson = lessonService.selectLessonById(id);
		mmap.put("lesson", lesson);
		return prefix + "/edit";
	}

	/**
	 * 修改保存lesson
	 */
	@RequiresPermissions("common:lesson:edit")
	@Log(title = "lesson", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ZyLesson lesson) {
		return toAjax(lessonService.updateLesson(lesson));
	}

	/**
	 * 删除lesson
	 */
	@RequiresPermissions("common:lesson:remove")
	@Log(title = "lesson", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(lessonService.deleteLessonByIds(ids));
	}
}
